package com.tgu.home_action.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tgu.home_action.pojo.state.RoleId;
import lombok.Data;


import java.util.List;
import java.util.Set;

@Data
public class Role {

    private Integer id;

    private String role;

    private List<Admin> admins;

    /**
     * 获得当前登录用户的身份
     */
    @JsonIgnore
    public static int getRoleId(Family familyUser, Volunteer volunteerUser, Admin adminUser) {
        if (familyUser != null) {
            return RoleId.FAMILY.getValue();
        }
        if (volunteerUser != null) {
            return RoleId.VOLUNTEER.getValue();
        }
        if (adminUser != null) {
            return getRoleId(adminUser.getRole());
        }
        return 0;
    }

    @Override
    public String toString() {
        return "Role{" +
                "id=" + id +
                ", role='" + role + '\'' +
                '}';
    }

    /**
     * 通过身份id获取身份名
     */
    @JsonIgnore
    public static String getRole(int roleId) {
        for (RoleId role : RoleId.values()) {
            if (role.getValue() == roleId) {
                return role.getRole();
            }
        }
        return "anonymity";
    }

    /**
     * 通过身份名获取身份ID
     */
    public static int getRoleId(String role) {
        for (RoleId roleId : RoleId.values()) {
            if (roleId.getRole().equals(role)) {
                return roleId.getValue();
            }
        }
        return 0;
    }
}
